﻿package com.rockylhc
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.utils.Timer;
	import org.flashdevelop.utils.FlashConnect;

	/**
	 * ...
	 * @author Rocky
	 */
	public class VidPlayer extends Sprite
	{
		private var _video:Video;
		private var _stream:NetStream;	
		private var _controller:Sprite;
		private var connection:NetConnection;
		private var autoPlay:Boolean=true;
		private var isPlaying:Boolean = true;
		private var isStop:Boolean = false;
		
		public static var _vidholder:Sprite;
		public static var _play:Sprite;
		public static var _pause:Sprite;
		public static var _stop:Sprite;
		public static var _progress:Sprite;
		public static var _seeker:Sprite;
		public static var _seekgrab:Sprite;
		public static var _mute:Sprite;
		public static var _unmute:Sprite;
		public static var _volumebar:Sprite;
		
		// time to buffer for the video in sec.
		private const BUFFER_TIME:Number = 8;
		// start volume when initializing player
		private const DEFAULT_VOLUME:Number = 0.6;
		// update delay in milliseconds.
		private const DISPLAY_TIMER_UPDATE_DELAY:int = 10;
		
		public var timerDisplay:Timer;
		public var vidInfo:Object;
		public var seeking:Boolean= false;
		
		public function VidPlayer()
		{
			//init;
		}
	
		public function SetAsset(VideoContainer:Sprite, PlayBtn:Sprite = null, PauseBtn:Sprite = null, StopBtn:Sprite = null, Progress:Sprite = null, PlaySeek:Sprite = null, SeekBar:Sprite = null, MuteBtn:Sprite = null, unMuteBtn:Sprite = null, VolumeControl:Sprite = null):void
		{		
			if (PlayBtn)
			{
				_controller.addChild(PlayBtn);
				_controller.addChild(PauseBtn);
			}
			if (StopBtn)
			{
				_controller.addChild(StopBtn);
			}
			if (Progress )
			{
				_controller.addChild(Progress);
			}
			if (PlaySeek )
			{
				_controller.addChild(SeekBar);
				_controller.addChild(PlaySeek);	
			}
			if (MuteBtn)
			{
				_controller.addChild(MuteBtn);
				_controller.addChild(unMuteBtn);
			}
			if (VolumeControl)
			{
				_controller.addChild(VolumeControl);
			}
			
			_controller.addChild(VideoContainer);
			//return _controller;
		}
		
		public function addVideo(flvURL:*, PlayerMode:*, vidWidth:Number = 320, vidHeight:Number = 240):*
		{
			_controller = new Sprite();
			
			connection = new NetConnection();
			connection.connect(null);
			
			_stream = new NetStream(connection);
			_stream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
			
			_video = new Video();
			
			_stream.play(flvURL);
			_stream.bufferTime = BUFFER_TIME;
			
			vidInfo = new Object();
			vidInfo.onMetaData = MetaDataHandler;
			_stream.client = vidInfo;
			
			_video.attachNetStream(_stream);
			_video.width = vidWidth;
			_video.height = vidHeight;
			_video.smoothing = true;
	
			_vidholder.x = vidWidth*0.5;
			_vidholder.y = vidHeight * 0.5;
			_vidholder.alpha = 0;
			
			_controller.addChild(_video);
			
			addChild(_controller);
			
			switch (PlayerMode) 
			{
				case "BasicMode":
				// w/o volume, play pause stop buffering with seek
				SetAsset(_vidholder, _play, _pause, _stop, _progress, _seeker, _seekgrab);
				
				_play.x = _play.width * 0.5;
				_play.y = vidHeight + _play.height * 0.5;
				
				_pause.x = _play.x;
				_pause.y = _play.y;
				
				_stop.x = _stop.width + _play.x;
				_stop.y = _play.y;
				_stop.height = _play.height;
				
				_progress.x = _play.width + _stop.width + _progress.width * 0.5 ;
				_progress.y = _play.y; 
				
				_seeker.x = _progress.x- _seeker.width*0.5;
				_seeker.y = _progress.y;
				_seeker.height = _play.height;
				
				_seekgrab.x = vidWidth*0.5+ _play.width;
				_seekgrab.y = _seeker.y;
				_seekgrab.width = vidWidth - _play.width - _stop.width;
				_seekgrab.height = _play.height;
				
				_mute.width = 0;
				
				break;
				case "StandardMode":
				SetAsset(_vidholder, _play, _pause, _stop, _progress, _seeker, _seekgrab, _mute, _unmute);
				// w/o volume control,  play pause stop mute buffering with seek
				_play.x = _play.width * 0.5;
				_play.y = vidHeight + _play.height * 0.5;
				
				_pause.x = _play.x;
				_pause.y = _play.y;
				
				_stop.x = _stop.width + _play.x;
				_stop.y = _play.y;
				_stop.height = _play.height;
				
				_mute.x = vidWidth - _mute.width*0.5;
				_mute.y = _play.y;
				
				_unmute.x = _mute.x;
				_unmute.y = _mute.y;
				
				_progress.x = _play.width + _stop.width + _progress.width ;
				_progress.y = _play.y; 
				
				_seeker.x = _progress.x- _seeker.width*0.5;
				_seeker.y = _progress.y;
				_seeker.height = _play.height;
				
				_seekgrab.x = vidWidth*0.5+ _play.width- _mute.width*0.5;
				_seekgrab.y = _seeker.y;
				_seekgrab.width = vidWidth - _play.width - _stop.width - _mute.width;
				_seekgrab.height = _play.height;
				break;
				case "CompleteMode":
				// play, pause, stop, mute, seek with buffering, volume control
				//SetAsset(_vidholder, _play, _stop, _progress,_mute,_volume);
				break;
			}
			
			_seeker.mouseChildren = false;
			_progress.mouseEnabled = false;
			_unmute.mouseEnabled = false;
			_unmute.alpha = 1;
			
			_pause.mouseEnabled = false;
			_pause.alpha = 1;
			
			_play.addEventListener(MouseEvent.CLICK, playpauseEvent);
			_play.buttonMode = true;
			
			_stop.addEventListener(MouseEvent.CLICK, stopEvent);
			_stop.buttonMode = true;
			
			_mute.addEventListener(MouseEvent.CLICK, muteEvent);
			_mute.buttonMode = true;
			
			_seekgrab.addEventListener(MouseEvent.MOUSE_DOWN, seekVideo);
			_seeker.addEventListener(MouseEvent.MOUSE_DOWN, seekVideo);
			stage.addEventListener(MouseEvent.MOUSE_UP, releaseVideo);
			_seekgrab.buttonMode = true;
			
			timerDisplay = new Timer(10);
			timerDisplay.addEventListener(TimerEvent.TIMER, updateDisplay);
			timerDisplay.start();
		}
		
		private function muteEvent(e:MouseEvent):void {
			
			
			if (_unmute.alpha == 0) {
				_unmute.alpha = 1;
				_stream.soundTransform	= new SoundTransform(1);
			}else {
				_unmute.alpha = 0;
				_stream.soundTransform	= new SoundTransform(0);
			}
			
		}
				
		private function seekVideo(e:MouseEvent):void 
		{
			seeking = true;
			_stream.pause();
			_seeker.x = mouseX;
			_seeker.startDrag(false, new Rectangle(_seekgrab.x-_seekgrab.width*0.5+_seeker.width*0.5, _seekgrab.y, _seekgrab.width-_seeker.width*0.8, 0));
		}
		
		private function releaseVideo(e:MouseEvent):void 
		{
			seeking = false;
			_seeker.stopDrag();
			_stream.resume();
			
		}

		private function updateDisplay(e:TimerEvent):void 
		{
			_progress.x = _play.width + _stop.width + _progress.width * 0.5 ;
			
			
			_progress.width	= _stream.bytesLoaded * (_vidholder.width - (_stop.width +_play.width + _mute.width)) / _stream.bytesLoaded  ;
			
			if (seeking)
			{
				_stream.seek((((_seeker.x+ _seeker.width * 0.5) - (_vidholder.width * 0.5 + _seeker.width * 0.5) * 0.5) * vidInfo.duration) / (_seekgrab.width-_seeker.width*0.5))+ (_seeker.width*0.75);
				}else{
				_seeker.x = (_stream.time * (_seekgrab.width-_seeker.width*0.5) / vidInfo.duration + (_vidholder.width*0.5+_seeker.width*0.5)*0.5);
			}
			
			if (_stream.time >= vidInfo.duration || isStop == true)
			{
				if (_vidholder.alpha >= .5)
				{
					_vidholder.alpha = .5;
					_pause.alpha = 0;
					_stream.pause();
					_stream.seek(0);
					isStop = true;
					isPlaying = false;
					}else{
					_vidholder.alpha += .015;
				}
				}else{
				_vidholder.alpha = 0;
			}
		}
		
		private function stopEvent(e:MouseEvent):void 
		{
			_vidholder.alpha = .5;
			_pause.alpha = 0;
			_stream.pause();
			_stream.seek(0);
			isPlaying = false;
			isStop = true;
			FlashConnect.trace("stop at ="+_stream.time);
		}
		
		private function playpauseEvent(e:MouseEvent):void 
		{
			if (isPlaying == true) {
				_stream.pause();
				isPlaying = false;
				_pause.alpha = 0;
			}else {
				_stream.resume();
				isPlaying = true;
				_pause.alpha = 1;
			}
			isStop = false;
		}
		
		public function MetaDataHandler(e:Object):void 
		{
			vidInfo = e;
		}
		
		public function onNetStatus(e:NetStatusEvent):void 
		{
		}
		
	
	}

}